package com.secretum;
// Copyright (C) 2001, Internet Designers Ltd. All rights reserved.
// This program is free software and may be distributed under the terms
// of the GNU Lesser General Public License, Version 2.1.
// This program comes with ABSOLUTELY NO WARRANTY.
//
// Neil Winton (neil.winton@idl-bt.com)
//
// $Id: STest.java,v 1.5 2001/06/21 14:53:36 wintonn Exp $

import java.io.*;
import java.net.*;
import com.secretum.zebedee.*;


/**
 * This is a simple demonstration of the Zebedee server-side API.
 */

public class STest
{

    public static void main(String args[]) throws Exception
    {
	// We create a "master" instance of the ZBDTunnelServer so that we
	// can set some options once and, more importantly, share the
	// client session key table between other instances.

	ZBDTunnelServer master = new ZBDTunnelServer();

	master.setKeySize(128);
	master.setCompression(ZBDTunnel.COMPRESSION_ZLIB, 9);

	// Get a listen socket on the Zebedee default TCP port.

	ServerSocket ss = new ServerSocket(ZBDTunnel.DFLT_TCP_PORT);

	// Loop round, creating a new thread to handle each incoming
	// connection. Pass the master ZBDTunnelServer instance to each
	// one.

	while (true)
	{
	    new ZServer(master, ss.accept()).start();
	}
    }
}

/**
 * This class handles a tunnel connection from a client.
 */

class ZServer extends Thread
{
    ZBDTunnelServer srv;    // Individual instance of a ZBDTunnelServer
    Socket sock;	    // The tunnel socket itself

    ZServer(ZBDTunnelServer master, Socket sock)
    {
	// Create a private instance of the ZBDTunnelServer based on the
	// master instances passed in.

	srv = new ZBDTunnelServer(master);
	this.sock = sock;

	// Lower our priority so that the accepting thread will not
	// block.

	setPriority(NORM_PRIORITY - 1);
    }

    public void run()
    {
	try
	{
	    // Try to negotiate a connection.
	    srv.connect(sock);
	}
	catch (Exception e)
	{
	    System.out.println("Error in connect: " + e);
	}

	// Start new threads to read and write data from and to
	// the tunnel.

	new ZBDTcpTunnelReader(srv, (Socket)srv.getTargetSocket()).start();
	new ZBDTcpTunnelWriter(srv, (Socket)srv.getTargetSocket()).start();
    }
}
